package com.jm.weibo.logic;

import java.util.List;

import weibo4andriod.Comment;
import weibo4andriod.DirectMessage;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jm.weibo.ui.R;

public class DirectMessagesAdapter extends BaseAdapter{
	
 
    public List<DirectMessage> directMessages;
	
	private Context context;

	private DirectMsgHolder directMsgHolder = new DirectMsgHolder();
	
	public DirectMessagesAdapter(Context con, List<DirectMessage> list) {
		this.directMessages = list;
		this.context = con;
	}

	public int getCount() {
		return directMessages.size() + 2;
	}

	public Object getItem(int position) {
		if (position == 0){
			return null;
		}else if (position == this.getCount() - 1){
			return null;
		}
		// 返回这条微薄对象
		else{
			return directMessages.get(position - 1);
		}
	}

	public long getItemId(int position) {
		if (position == 0){// 刷新
			return 0;
		} else if (position > 0 && position < this.getCount() - 1){// 返回微博的ID
			return directMessages.get(position - 1).getId();
		} else { // 选中了更多
			return -1;
		}
	}

	/**
	 * 动态给StatusAdapter添加数据
	 * @param adddata
	 */
	public void addMoreData(List<DirectMessage> adddata) {
		this.directMessages.addAll(adddata);
		this.notifyDataSetChanged();
	}

	
	

	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == 0)// 刷新
		{
			View dMsgItem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) dMsgItem.findViewById(R.id.tvItemContent);
			tv.setText("刷新");
			return dMsgItem;
		} else if (position == this.getCount() - 1)// 更多
		{
			View dMsgItem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) dMsgItem.findViewById(R.id.tvItemContent);
			tv.setText("更多");
			return dMsgItem;
		} else {
			View dMsgItem = null;

			if (convertView != null && convertView.findViewById(R.id.tvItemName) != null) {
				Log.d("getview", "doGetView-------get TextView-----------"
						+ position);
				dMsgItem = convertView;
			} else {
				Log.d("getview", "doGetView-------new TextView-----------"
						+ position);
				// 把xml布局文件变成View对象
				dMsgItem = LayoutInflater.from(context).inflate(R.layout.itemview_direct_msg_to_me, null);
			}
			
			this.initWeiboHoler(dMsgItem);
			
			DirectMessage directMessage = this.directMessages.get(position-1);
			this.directMsgHolder.tvIDMsgUsername.setText(directMessage.getRecipientScreenName() == null ? "未知" : directMessage.getRecipientScreenName());
			this.directMsgHolder.tvDMsgContent.setText(directMessage.getText());
			this.directMsgHolder.tvIDMsgDate.setText(directMessage.getCreatedAt().toLocaleString());
			return dMsgItem;
		}
	}

	private void initWeiboHoler(View dMsgItem) {
		this.directMsgHolder.ivItemDMsgPortrait = (ImageView) dMsgItem.findViewById(R.id.ivItemDMsgPortrait);
		this.directMsgHolder.tvIDMsgUsername = (TextView) dMsgItem.findViewById(R.id.tvItemDMsgName);
		this.directMsgHolder.tvDMsgContent = (TextView) dMsgItem.findViewById(R.id.tvItemDMsgContent);
	    this.directMsgHolder.tvIDMsgDate = (TextView) dMsgItem.findViewById(R.id.tvItemDMsgDate);
	}
}
